package com.scsj.supermarket.view.activity.settingmodel;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.scsj.supermarket.R;
import com.scsj.supermarket.base.ChangeBindPhoneBase;
import com.scsj.supermarket.bean.BaseBean;
import com.scsj.supermarket.presenter.ChangeBindPhonePresenter;
import com.scsj.supermarket.utils.Tool;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;

/**
 * 设置新手机号
 */
public class SetNewPhoneNumberActivity extends BaseActivity implements View.OnClickListener, ChangeBindPhoneBase.IView {

    private String mOldPhone;
    private String mOldSmsCode;
    /**
     * 请输入新手机号
     */
    private EditText mPhoneEt;
    /**
     * 获取验证码
     */
    private Button mSendCodeBtn;
    /**  */
    private EditText mRegisterCodeEt;
    /**
     * 确认修改
     */
    private Button mCommitBtn;
    private ChangeBindPhonePresenter presenter;
    private String mPhone;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_set_new_phone_number);
        //ScreenAdapterTools.getInstance().loadView((ViewGroup) getWindow().getDecorView());
        //ScreenAdapterTools.getInstance().reset(this);//如果希望android7.0分屏也适配的话,加上这句
        initToolbar();
        setToolbarTitle("设置新手机号");
        hasBack(true);
        Intent intent = getIntent();
        mOldPhone = intent.getStringExtra("oldPhone");
        mOldSmsCode = intent.getStringExtra("oldSmsCode");
        presenter = new ChangeBindPhonePresenter(this);

    }

    @Override
    protected void initView() {

        mPhoneEt = (EditText) findViewById(R.id.phone_et);
        mSendCodeBtn = (Button) findViewById(R.id.send_code_btn);
        mSendCodeBtn.setOnClickListener(this);
        mRegisterCodeEt = (EditText) findViewById(R.id.register_code_et);
        mCommitBtn = (Button) findViewById(R.id.commit_btn);
        mCommitBtn.setOnClickListener(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.send_code_btn:
                mPhone = mPhoneEt.getText().toString().trim();
                if (!Tool.isPhoneNumber(mPhone)) {
                    Toasty.info(this, "请输入正确的手机号").show();
                    return;
                }

                JSONObject object1 = new JSONObject();
                object1.put("phone", mPhone);
                RequestBody body = RequestBody.create(okhttp3.MediaType
                        .parse("application/json;charset=utf-8"), object1.toString());
                presenter.deliverSendSmsCodeData(body);
                break;
            case R.id.commit_btn:
                mPhone = mPhoneEt.getText().toString().trim();
                if (!Tool.isPhoneNumber(mPhone)) {
                    Toasty.info(this, "请输入正确的手机号").show();
                    return;
                }
                String mRegisterCode = mRegisterCodeEt.getText().toString().trim();
                if(mRegisterCodeEt.getText() == null || mRegisterCode.length() != 6){
                    Toasty.info(this,"请输入完整的验证码").show();
                    return;
                }

                JSONObject object = new JSONObject();
                object.put("newCode", mRegisterCode);
                object.put("newPhone", mPhone);
                object.put("oldCode", mOldSmsCode);
                object.put("username", mOldPhone);
                RequestBody body1 = RequestBody.create(okhttp3.MediaType
                        .parse("application/json;charset=utf-8"), object.toString());
                presenter.deliverVerifySmsCodeData(body1);
                break;
        }
    }

    @Override
    public void sendSmsCodeSuccessMsg(String s, BaseBean addDABean) {
        Toasty.success(this, s).show();
    }

    @Override
    public void verifySmsCodeSuccessMsg(String s, BaseBean deleteDABean) {
        Toasty.success(this,s).show();
    }

    @Override
    public void showErrorMsg(String s) {
        Toasty.error(this,s).show();
    }
}
